<template>
  <div class="main" style="margin: 10px">
    <el-card>
      <div>
        <!--工具栏-->
        <div class="head-container">
          <div>
            <span>
              <span class="filter-item">
                <el-button type="primary" icon="el-icon-plus" @click="showAddDialog">新增</el-button>
              </span>
            </span>
          </div>
        </div>
      </div>
      <el-card style="margin-top: 10px">
        <el-table border ref="table" :data="tableData" style="width: 100%" row-key="id" lazy>>
          <el-table-column type="index" label="#" width="55"> </el-table-column>
          <el-table-column prop="name" label="名称"> </el-table-column>
          <el-table-column prop="dsType" label="类型" align="center"></el-table-column>
          <el-table-column prop="username" label="用户名" align="center"></el-table-column>
          <el-table-column prop="password" label="密码" align="center"></el-table-column>
          <el-table-column prop="confType" label="配置方式" align="center">
                <template slot-scope="scope">                    
                    <p v-if="scope.row.confType=='0'">JDBC</p>
                    <p v-if="scope.row.confType=='1'">主机</p>
                </template>
		      </el-table-column>
          <el-table-column prop="createTime" label="创建日期" align="center">
            <template slot-scope="scope">
              {{ scope.row.createTime | formatDate }}
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="更新日期" align="center">
            <template slot-scope="scope">
              {{ scope.row.createTime | formatDate }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150" align="center">
            <template slot-scope="scope">
              <el-button type="primary" @click="showEditDialog(scope.row)">编辑</el-button>
              <el-button type="danger" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </el-card>
    <!-- 分页 -->
    <pagination :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList"/>
    <DataSourceDialog :dataSourceDialog='dataSourceDialog' :formData='formData' @addDataSource='addDataSource' @editDataSource='editDataSource'></DataSourceDialog>
  </div>
</template>

<script>
import { getDictPageList, addDataSource, updateDataSource, deleteSource } from "@/api/system/dataSource";
import Pagination from "@/components/Pagination"; // el分页
import DataSourceDialog from "@/components/System/gen/dataSourceDialog";

export default {
  data() {
    return {
      AddBtnAuth: ["add"],
      tableData: [],
      searchKey: "",
      loading: false,
      dataSourceDialog: {
        show: false,
        title: ""
      },
      //条件查询搜索
      total: 0,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
      },
      formData: {},
    };
  },
  methods:{
    getList() {
      getDictPageList(this.listQuery).then((res) => {
        this.tableData = res.data.records;
        this.total = res.data.total;
        this.listQuery.pageNum = res.data.current;
        this.listQuery.pageSize = res.data.size;

        setTimeout(() => {
          this.listLoading = false;
        }, 1.5 * 1000);
      });
    },
    // 新增按钮触发
    showAddDialog() {
      this.dataSourceDialog = {
        show: true,
        title: "新增数据源",
        option: 'add'
      }
      this.formData = {
      }
    },
    //编辑
    showEditDialog(row) {
      this.dataSourceDialog = {
        show: true,
        title: "修改数据源",
        option: 'edit'
      }
      this.formData = row
    },
    //删除
    handleDelete(row) {
      this.$confirm('此操作将永久删除数据类型为"' + row.type + '"', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteSource(row.id).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getList();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //新增提交
    addDataSource() {
      addDataSource(this.formData).then(res => {
        this.rolesDialog.show = false
        this.$notify({
          title: '成功',
          message: '新增数据源成功',
          type: 'success'
        });
        this.getList()
      })
    },
    //编辑提交
    editDataSource() {
      updateDataSource(this.formData).then(res => {
        this.rolesDialog.show = false
        this.$notify({
          title: '成功',
          message: '修改数据源成功',
          type: 'success'
        });
        this.getList()
      })
    }
  },
  mounted() {
    this.getList();
  },
  components: {
    Pagination,
    DataSourceDialog,
  },
}
</script>

<style lang="scss" scoped>
::v-deep .el-switch.is-disabled {
  opacity: 1;
}
::v-deep .el-switch.is-checked .el-switch__core {
  &:hover {
    cursor: pointer;
  }
}
// 工具栏
.head {
  &-container {
    text-indent: -10px;
    > div {
      display: flex;
      align-items: center;
      justify-content: space-between;
      > span {
        display: flex;
        align-items: center;
      }
      .filter {
        &-item {
          margin-left: 10px;
        }
      }
    }
  }
}
</style>